// pages/manage/activityManage/activityAdd.js
import {
    getCurrentDateTime
} from '../../../../utils/time.js'
import {
    addActivity,
    getOptions,
    getClassList,
    getMajorBySearch,
    getDepartmentBySearch,
} from '../../../../utils/request/manage/activityManage/activityManage'
import Toast from 'tdesign-miniprogram/toast/index';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        value: null,
        activityStartVisible: false,
        activityEndVisible: false,
        registrationStartVisible: false,
        registrationEndVisible: false,
        peopleVisible: false,

        activityForm: {
            title: '',
            sponsor: '',
            typeId: 6,
            creator: '',
            activityId: '',
            activityLocation: '',
            registrationStart: getCurrentDateTime(),
            registrationEnd: getCurrentDateTime(),
            time: '',
            userNumber: '',
            number: 0,
            rangeList: [],
            integral: 0,
            posterUrl: '',
            stateId: '',
            content: '',
            activityStart: getCurrentDateTime(),
            activityEnd: getCurrentDateTime(),
            updateTime: '',
            createTime: '',
            signInWay: '0',
            homeLabour: '0'
        },
        classSearchForm: {
            departmentId: null,
            majorId: null,
            departmentName: '',
            majorName: ''
        },
        number: '',
        integral: '',
        titleNumber: '活动人数',
        titleIntegral: '活动积分',
        typeNameVisible: false,
        typeNameList: [{
                "label": "创新创业",
                "value": 1
            },
            {
                "label": "专业实践",
                "value": 2
            },
            {
                "label": "志愿服务",
                "value": 3
            },
            {
                "label": "文体活动",
                "value": 4
            },
            {
                "label": "勤工助学",
                "value": 5
            },
            {
                "label": "生活技能",
                "value": 6
            },
            {
                "label": "榜样引领",
                "value": 7
            }
        ],
        signWay: ['手机签到', '终端指纹', '终端刷卡'],
        options: [],
        classTable: [],
        peopleCheckBox: [],
        optionsVisible: false
    },
    // 提交活动
    handleAddActivity() {
        console.log(this.data.activityForm)
        addActivity(this.data.activityForm).then(res => {
            console.log(res)
            if (res !== undefined && res.code === 200) {
                Toast({
                    context: this,
                    selector: '#t-toast',
                    message: '发布成功',
                    icon: 'check-circle',
                    direction: 'column',
                });
            } else {
                Toast({
                    context: this,
                    selector: '#t-toast',
                    message: res.data.message,
                    icon: 'erroe-circle',
                    direction: 'column',
                });
            }
        })
    },
    // 获取报名人群
    getOptions() {
        console.log('正在获取报名人群')
        getOptions().then(resp => {
            console.log(resp)
            this.setData({
                options: resp.data
            })
        })
    },
    // 检索班级
    getClass() {
        console.log(this.data.classSearchForm)
        getClassList(this.data.classSearchForm).then(res => {
            this.setData({
                classTable: res.data.data.tableData
            })
        })
    },
    onPeopleChange(event) {
        console.log('checkbox', event.detail.value);
        this.setData({
            peopleCheckBox: event.detail.value,
            'activityForm.rangeList': event.detail.value.slice(0, 3).map(element => [element])
        })
    },
    // 输入班级
    inputClass(event) {
        this.setData({
            'classSearchForm.className': event.detail.value
        })
        this.getClass()
    },
    // 输入部门
    inputDepartment(event) {
        this.setData({
            'classSearchForm.departmentName': event.detail.value
        })
        this.getClass()
    },
    // 输入专业
    inputMajor(event) {
        this.setData({
            'classSearchForm.majorName': event.detail.value
        })
        this.getClass()
    },
    onPeopleVisibleChange() {
        this.setData({
            peopleVisible: false
        })
    },
    onChangeOptions() {
        this.setData({
            optionsVisible: true
        })
    },
    showPeople() {
        this.setData({
            peopleVisible: true
        })
    },
    handleTitle(e) {
        this.setData({
            'activityForm.title': e.detail.value
        })
    },
    handleLocation(e) {
        this.setData({
            'activityForm.activityLocation': e.detail.value
        })
    },
    handleContent(e) {
        this.setData({
            'activityForm.content': e.detail.value
        })
    },
    handleSponsor(e) {
        this.setData({
            'activityForm.sponsor': e.detail.value
        })
    },
    handleHomeLabour(e) {
        this.setData({
            'activityForm.homeLabour': e.detail.value
        })
    },
    handleSignInWay(e) {
        this.setData({
            'activityForm.signInWay': e.detail.value
        })
    },
    onTypeNamePicker() {
        this.setData({
            typeNameVisible: true
        })
    },
    onTypeNamePickerCancel() {
        this.setData({
            typeNameVisible: false
        })
    },
    onTypeNameChange(e) {
        this.setData({
            'activityForm.typeId': e.detail.value[0],
            'activityForm.typeName': e.detail.label[0]
        })
    },
    onIntegralChange(e) {
        const {
            value
        } = e.detail;
        this.setData({
            'activityForm.integral': value
        });
    },
    showPicker(e) {
        const {
            mode
        } = e?.currentTarget?.dataset;
        this.setData({
            mode,
            [`${mode}Visible`]: true,
        });
    },
    // -------------  活动人数
    handlerNumber(e) {
        this.setData({
            'activityForm.number': e.detail.value
        })
    },

    // --------------  活动开始时间
    showActivityStart() {
        console.log('活动开始时间')
        this.setData({
            activityStartVisible: true,
        });
    },
    cancelActivityStart() {
        const {
            mode
        } = this.data;
        this.setData({
            activityStartVisible: false,
        });
    },
    confirmActivityStart(e) {
        const {
            value
        } = e?.detail;
        const {
            mode
        } = this.data;

        console.log('confirm', value);

        this.setData({
            'activityForm.activityStart': value,
            activityStartVisible: false,
            [`${mode}Text`]: value,
        });

    },
    // ---------------  活动结束时间
    showActivityEnd() {
        console.log('活动结束时间')
        this.setData({
            activityEndVisible: true,
        });
    },
    cancelActivityEnd() {
        const {
            mode
        } = this.data;
        this.setData({
            activityEndVisible: false,
        });
    },
    confirmActivityEnd(e) {
        const {
            value
        } = e?.detail;
        const {
            mode
        } = this.data;

        console.log('confirm', value);

        this.setData({
            'activityForm.activityEnd': value,
            activityEndVisible: false,
            [`${mode}Text`]: value,
        });


    },
    // -------------------------  报名开始时间
    showRegistrationStart() {
        console.log(getCurrentDateTime())
        this.setData({
            registrationStartVisible: true
        });
    },
    cancelRegistrationStart() {
        const {
            mode
        } = this.data;
        this.setData({
            registrationStartVisible: false,
        });
    },
    confirmRegistrationStart(e) {
        const {
            value
        } = e?.detail;
        const {
            mode
        } = this.data;

        console.log('confirm', value);

        this.setData({
            'activityForm.registrationStart': value,
            registrationStartVisible: false,
            [`${mode}Text`]: value,
        });


    },
    // -------------------------  报名结束时间
    showRegistrationEnd() {
        this.setData({
            registrationEndVisible: true,
        });
    },
    cancelRegistrationEnd() {
        const {
            mode
        } = this.data;
        this.setData({
            registrationEndVisible: false,
        });
    },
    confirmRegistrationEnd(e) {
        const {
            value
        } = e?.detail;
        const {
            mode
        } = this.data;

        console.log('confirm', value);

        this.setData({
            'activityForm.registrationEnd': value,
            registrationEndVisible: false,
            [`${mode}Text`]: value,
        });
    },

    // 打开院系选择框
    onDepartmentPicker() {
        this.setData({
            departmentPicker: true
        })
        getDepartmentBySearch().then(res => {
            let modifiedData = res.data.data.tableData.map(item => {
                return {
                    label: item.departmentName,
                    value: item.departmentId
                };
            });
            // 使用 setData 方法更新数据
            this.setData({
                departmentTable: modifiedData
            });
        })
    },
    // 打开专业选择框
    onMajorPicker() {
        this.setData({
            majorPicker: true
        })
        const form = {
            departmentId: this.data.classSearchForm.departmentId,
            pageSize: -1,
            pageNo: -1
        }
        getMajorBySearch(form).then(res => {
            let modifiedData = res.data.data.tableData.map(item => {
                return {
                    label: item.majorName,
                    value: item.majorId
                }
            })
            this.setData({
                majorTable: modifiedData
            })
        })
    },

    // 选择院系
    onDepartmentChange(event) {
        this.setData({
            'classSearchForm.departmentId': event.detail.value[0],
            'classSearchForm.departmentName': event.detail.label[0]
        });
        this.getClass()
    },
    // 选择专业
    onMajorChange(event) {
        this.setData({
            'classSearchForm.majorId': event.detail.value[0],
            'classSearchForm.majorName': event.detail.label[0]
        })
        this.getClass()
    },
    // 取消院系选择
    onDepartmentCancel() {
        this.setData({
            departmentPicker: false
        })
    },
    // 取消班级选择
    onMajorCancel() {
        this.setData({
            majorPicker: false
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {

        this.getOptions()
        this.getClass()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})